package controller

import (
	"github.com/savsgio/atreugo/v11"
	"gowork/hotter/service"
)

type HotterController struct {
	HotterService *service.HotterService
}

func NewHotterController() *HotterController {
	return &HotterController{
		HotterService: service.NewHotterService(),
	}
}

func (hc *HotterController) HotRenewal(ctx *atreugo.RequestCtx) error {

	versionNumber := string(ctx.FormValue("versionNumber"))
	platform := string(ctx.FormValue("platform"))

	res := hc.HotterService.HotRenewal(versionNumber, platform)
	if res["data"] != nil {

		fileName := (res["data"].(map[string]string))["fileName"]
		//paths := strings.Split(fileName, "\\")

		ctx.SetContentType("aContent-Disposition")
		//强制浏览器下载
		ctx.Response.Header.Set("Content-Disposition", "attachment; filename="+(res["data"].(map[string]string))["paths"])
		//浏览器下载或预览
		ctx.Response.Header.Set("Content-Disposition", "inline;filename="+(res["data"].(map[string]string))["paths"])
		ctx.Response.Header.Set("Content-Transfer-Encoding", "binary")
		ctx.Response.Header.Set("Cache-Control", "no-cache")

		ctx.SendFile(fileName)

	} else {

		return ctx.JSONResponse(res)
	}

	return nil
}
